/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ourgame.tankgame.control;

import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import com.ourgame.tankgame.bullet.Bullet;
import com.ourgame.tankgame.attribute.BulletAttribute;
import com.ourgame.tankgame.bullet.NormalBullet;
import com.ourgame.tankgame.tank.Tank;
import gameutil.Globals;

/**
 *
 * @author echoff
 */
public class BaseWeaponSystem extends WeaponSystem {

    Spatial bulletModel;
    private boolean inited;


    @Override
    public void initWeaponSystem() {

        bulletModel = controler.getAssetManager().loadModel(Globals.BULLET_MODEL_PATH);

        Tank tank = null;
        if (spatial instanceof Tank) {
            tank = (Tank) spatial;
        }

        if (null == tank) {
            throw new java.lang.RuntimeException("The bullet must form a Tank");
        }

        BulletAttribute bullet = new BulletAttribute("bullet");
        bullet.setBulletMass(0.01f);
        bullet.setBulletPower(1);
        bullet.setBulletSpeed(200f);
        bullet.setBulletRole(tank.getTankRole());

        equipBullet(bullet);
        
        inited = true;
        
    }

    @Override
    public void fire() {
        if (inited) {
            Bullet bullet = new NormalBullet(currentBulletAttribute, bulletModel.clone());

            Quaternion rot = spatial.getWorldRotation();
            Vector3f dir = rot.getRotationColumn(2);

            bullet.setBulletDir(dir);
            bullet.setBulletPos(spatial.getLocalTranslation().addLocal(0, 0.5f, 0));
            bullet.setBulletRot(rot);

            spatial.getParent().attachChild(bullet);
            controler.getPhysicsSpace().add(bullet);
        }
    }
}
